﻿using DevComponents.DotNetBar.Controls;
using NPOI.SS.Formula.Functions;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
using System.Linq;
using System.Net.NetworkInformation;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace AgvDemo
{
    public class MainManagerClass
    {
        //AgvPublic.TcpServer tcpManager;//TCP实例
        BLL.Agv_Device agvDeviceBll = new BLL.Agv_Device();
        public static List<Model.Agv_Device> agvDeviceList = new List<Model.Agv_Device>();


        public static BLL.Fault_Record faultRecordBll = new BLL.Fault_Record();
        public static List<Model.Fault_Record> faultRecordList = new List<Model.Fault_Record>();


        BLL.Traffic_Manager trafficManagerBll = new BLL.Traffic_Manager();
        public static List<Model.Traffic_Manager> trafficManagerList = new List<Model.Traffic_Manager>();

        BLL.Map_Card mapCardBll = new BLL.Map_Card();
        public static List<Model.Map_Card> mapCardList = new List<Model.Map_Card>();



        AgvFunctionClass agvFunc;
        public static TrafficManagerClass trafficManager;

        public static MonitorShowClass agvShow;
      
        public static AgvDispatchClass agvDispatch;

        public static Dictionary<int, PlcManagerClass> plcMDic = new Dictionary<int, PlcManagerClass>();

        public static BasicFunctionClass basicFunc;

     
        //public static ModbusLampClass mlc231;
        //public static ModbusLampClass mlc232;
        //public static ModbusLampClass mlc233;
        //public static ModbusLampClass mlc234;


        public static ModbusBoxClass boxAgv;

        public static ModbusTcpClass mtc;

        public static List<Thread> thList = new List<Thread>();


        public static object lockOmron = new object();
        public MainManagerClass()
        {
          
            agvFunc = AgvFunctionClass.GetInstance();
            basicFunc = new BasicFunctionClass();
            trafficManager = TrafficManagerClass.GetInstance();
            agvShow = new MonitorShowClass();
                 
            agvDispatch = new AgvDispatchClass();

            mtc = new ModbusTcpClass("10.185.220.30");
            //mlc231 = new ModbusLampClass("10.185.221.231");
            //mlc232 = new ModbusLampClass("10.185.221.232");
            //mlc233 = new ModbusLampClass("10.185.221.233");
            //mlc234 = new ModbusLampClass("10.185.221.234");

            boxAgv = new ModbusBoxClass("10.185.221.238");


            BindAgv();
            BindTraffic();
            BindMap();
            BindSinal();
            IniAgvPlc();
            IniCheckThread();

        }

        Thread thMonitor;
        Thread thSign;
        public void IniCheckThread()
        {

            thSign = new Thread(new ThreadStart(thSignal));
            thSign.IsBackground = true;
            thSign.Start();

            thMonitor = new Thread(new ThreadStart(thMonitorMe));
            thMonitor.IsBackground = true;
            thMonitor.Start();
        }


        public void thMonitorMe()
        {
            while (true)
            {
                try
                {
                    foreach (int item in plcMDic.Keys)
                    {
                        if (plcMDic[item].isConnectSure)
                        {
                            agvShow.AgvMonitorShow(plcMDic[item].chargeState9, agvFunc);
                        }
                    }

                    Thread.Sleep(1000);


                }
                catch
                { }
            }


        }


        public void thSignal()
        {
            while (true)
            {
                try
                {
                    if (mtc.DoorUpLeaveControl == 1)
                    {
                        if (mtc.js.UpAgvLeave == 0)
                        {
                            mtc.DoorUpAgv = 0;
                            mtc.DoorUpLeaveControl = 0;


                        }
                        else
                        {
                            try
                            {
                                byte[] tempV = { 0, 0 };
                                mtc.MBmaster_Write(3, tempV);
                            }
                            catch
                            { }
                        }
                    }



                    if (mtc.DoorDownLeaveControl == 1)
                    {

                        if (mtc.js.DownAgvLeave == 0)
                        {
                            try
                            {
                                byte[] tempV = { 0, 1 };
                                mtc.MBmaster_Write(11, tempV);



                            }
                            catch
                            { }

                        }
                        else
                        {
                            if (mtc.js.DownAgvArrive == 0)
                            {
                                mtc.DoorDownAgv = 0;
                                mtc.DoorDownLeaveControl = 0;

                            }
                            else
                            {
                                try
                                {
                                    byte[] tempV = { 0, 0 };
                                    mtc.MBmaster_Write(9, tempV);
                                }
                                catch
                                { }


                            }


                        }
                    }




                    Thread.Sleep(1000);


                }
                catch
                { }
            }


        }




        public void IniAgvPlc()
        {
            try
            {
                if (agvDeviceList.Count > 0)
                {
                    for (int i = 0; i < agvDeviceList.Count; i++)
                    {
                        PlcManagerClass pmc = new PlcManagerClass(agvDeviceList[i].agv_id, agvDeviceList[i].agv_ip, agvFunc);
                        plcMDic.Add(agvDeviceList[i].agv_id, pmc);

                        AgvFunctionClass.TrafficDic.Add(agvDeviceList[i].agv_id, 0);

                        AgvFunctionClass.AgvOldStationDic.Add(agvDeviceList[i].agv_id, 0);

                        AgvFunctionClass.AgvErrorDic.Add(agvDeviceList[i].agv_id, 0);
                    }
                  
                }
            }
            catch
            {
            }
         
        }

     
       

      


        private void BindTraffic()
        {

            trafficManagerList = trafficManagerBll.GetModelList("");


            for (int i = 0; i < trafficManagerList.Count; i++)
            {
                int index = MainForm.mainForm.dgTraffic.Rows.Add();
                MainForm.mainForm.dgTraffic.Rows[index].ReadOnly = true;
               
                MainForm.mainForm.dgTraffic.Rows[index].Cells[0].Value = trafficManagerList[i].traffic_area_no;
                MainForm.mainForm.dgTraffic.Rows[index].Cells[1].Value = trafficManagerList[i].lock_card_num;
                MainForm.mainForm.dgTraffic.Rows[index].Cells[2].Value = trafficManagerList[i].except_card_for_unlock;
                if (trafficManagerList[i].card_direction == 1)
                {
                    MainForm.mainForm.dgTraffic.Rows[index].Cells[3].Value = "Foward";
                    MainForm.mainForm.dgTraffic.Rows[index].DefaultCellStyle.ForeColor = Color.White;
                }

                else
                {
                    MainForm.mainForm.dgTraffic.Rows[index].Cells[3].Value = "Back";
                    MainForm.mainForm.dgTraffic.Rows[index].DefaultCellStyle.ForeColor = Color.Fuchsia;
                }


                MainForm.mainForm.dgTraffic.Rows[index].Cells[4].Value = trafficManagerList[i].id;

            }

            MainForm.mainForm.dgTraffic.ClearSelection();

        }



        public void BindAgv()
        {

            agvDeviceList = agvDeviceBll.GetModelList("");


            for (int i = 0; i < agvDeviceList.Count; i++)
                {
                    int index = MainForm.mainForm.dgLine.Rows.Add();
                    MainForm.mainForm.dgLine.Rows[index].ReadOnly = true;
                    MainForm.mainForm.dgLine.Rows[index].Cells[0].Value = agvDeviceList[i].agv_id;
                    MainForm.mainForm.dgLine.Rows[index].Cells[1].Value = agvDeviceList[i].agv_ip;
                    if (agvDeviceList[i].agv_workline == 1)
                    {
                        MainForm.mainForm.dgLine.Rows[index].Cells[2].Value = "SPS";
                        MainForm.mainForm.dgLine.Rows[index].DefaultCellStyle.ForeColor = Color.White;
                    }

                    else if (agvDeviceList[i].agv_workline == 2)
                    {
                        MainForm.mainForm.dgLine.Rows[index].Cells[2].Value = "发变分装线";
                        MainForm.mainForm.dgLine.Rows[index].DefaultCellStyle.ForeColor = Color.White;
                    }
                    else if (agvDeviceList[i].agv_workline == 3)
                    {
                        MainForm.mainForm.dgLine.Rows[index].Cells[2].Value = "仪表线";
                        MainForm.mainForm.dgLine.Rows[index].DefaultCellStyle.ForeColor = Color.White;
                    }
                    else if (agvDeviceList[i].agv_workline == 4)
                    {
                        MainForm.mainForm.dgLine.Rows[index].Cells[2].Value = "车门线";
                        MainForm.mainForm.dgLine.Rows[index].DefaultCellStyle.ForeColor = Color.White;
                    }
                    else if (agvDeviceList[i].agv_workline == 5)
                    {
                        MainForm.mainForm.dgLine.Rows[index].Cells[2].Value = "前滑柱分装线";
                        MainForm.mainForm.dgLine.Rows[index].DefaultCellStyle.ForeColor = Color.White;
                    }
                    else if (agvDeviceList[i].agv_workline == 6)
                    {
                        MainForm.mainForm.dgLine.Rows[index].Cells[2].Value = "后轴分装线";
                        MainForm.mainForm.dgLine.Rows[index].DefaultCellStyle.ForeColor = Color.White;
                    }
                    else if (agvDeviceList[i].agv_workline == 7)
                    {
                        MainForm.mainForm.dgLine.Rows[index].Cells[2].Value = "动总分装线";
                        MainForm.mainForm.dgLine.Rows[index].DefaultCellStyle.ForeColor = Color.White;
                    }

                }

                MainForm.mainForm.dgLine.ClearSelection();
        }


        public void BindSinal()
        {

            MainForm.mainForm.dgSinal.Rows.Clear();
            for (int i = 0; i < 13; i++)
            {
                if (i == 0)
                {
                    int index = MainForm.mainForm.dgSinal.Rows.Add();
                    MainForm.mainForm.dgSinal.Rows[index].Cells[0].Value = "上线机构允许进入";
                    MainForm.mainForm.dgSinal.Rows[index].Cells[1].Value = "0";
                    MainForm.mainForm.dgSinal.Rows[index].Cells[2].Value = "强制1";
                    MainForm.mainForm.dgSinal.Rows[index].Cells[3].Value = "强制0";
                    MainForm.mainForm.dgSinal.Rows[index].DefaultCellStyle.ForeColor = Color.White;
                }
                else if (i == 1)
                {
                    int index = MainForm.mainForm.dgSinal.Rows.Add();
                    MainForm.mainForm.dgSinal.Rows[index].Cells[0].Value = "上线AGV到位";
                    MainForm.mainForm.dgSinal.Rows[index].Cells[1].Value = "0";
                    MainForm.mainForm.dgSinal.Rows[index].Cells[2].Value = "强制1";
                    MainForm.mainForm.dgSinal.Rows[index].Cells[3].Value = "强制0";
                    MainForm.mainForm.dgSinal.Rows[index].DefaultCellStyle.ForeColor = Color.White;
                }
                else if (i == 2)
                {
                    int index = MainForm.mainForm.dgSinal.Rows.Add();
                    MainForm.mainForm.dgSinal.Rows[index].Cells[0].Value = "上线机构允许离开";
                    MainForm.mainForm.dgSinal.Rows[index].Cells[1].Value = "0";
                    MainForm.mainForm.dgSinal.Rows[index].Cells[2].Value = "强制1";
                    MainForm.mainForm.dgSinal.Rows[index].Cells[3].Value = "强制0";
                    MainForm.mainForm.dgSinal.Rows[index].DefaultCellStyle.ForeColor = Color.White;
                }
                else if (i == 3)
                {
                    int index = MainForm.mainForm.dgSinal.Rows.Add();
                    MainForm.mainForm.dgSinal.Rows[index].Cells[0].Value = "上线AGV离开";
                    MainForm.mainForm.dgSinal.Rows[index].Cells[1].Value = "0";
                    MainForm.mainForm.dgSinal.Rows[index].Cells[2].Value = "强制1";
                    MainForm.mainForm.dgSinal.Rows[index].Cells[3].Value = "强制0";
                    MainForm.mainForm.dgSinal.Rows[index].DefaultCellStyle.ForeColor = Color.White;
                }
                else if (i == 4)
                {
                    int index = MainForm.mainForm.dgSinal.Rows.Add();
                    MainForm.mainForm.dgSinal.Rows[index].Cells[0].Value = "上线AGV故障";
                    MainForm.mainForm.dgSinal.Rows[index].Cells[1].Value = "0";
                    MainForm.mainForm.dgSinal.Rows[index].Cells[2].Value = "强制1";
                    MainForm.mainForm.dgSinal.Rows[index].Cells[3].Value = "强制0";
                    MainForm.mainForm.dgSinal.Rows[index].DefaultCellStyle.ForeColor = Color.White;
                }
                else if (i == 5)
                {
                    int index = MainForm.mainForm.dgSinal.Rows.Add();
                    MainForm.mainForm.dgSinal.Rows[index].Cells[0].Value = "上线机构故障";
                    MainForm.mainForm.dgSinal.Rows[index].Cells[1].Value = "0";
                    MainForm.mainForm.dgSinal.Rows[index].Cells[2].Value = "强制1";
                    MainForm.mainForm.dgSinal.Rows[index].Cells[3].Value = "强制0";
                    MainForm.mainForm.dgSinal.Rows[index].DefaultCellStyle.ForeColor = Color.White;
                }
                else if (i == 6)
                {
                    int index = MainForm.mainForm.dgSinal.Rows.Add();
                    MainForm.mainForm.dgSinal.Rows[index].Cells[0].Value = "下线机构允许进入";
                    MainForm.mainForm.dgSinal.Rows[index].Cells[1].Value = "0";
                    MainForm.mainForm.dgSinal.Rows[index].Cells[2].Value = "强制1";
                    MainForm.mainForm.dgSinal.Rows[index].Cells[3].Value = "强制0";
                    MainForm.mainForm.dgSinal.Rows[index].DefaultCellStyle.ForeColor = Color.White;
                }
                else if (i == 7)
                {
                    int index = MainForm.mainForm.dgSinal.Rows.Add();
                    MainForm.mainForm.dgSinal.Rows[index].Cells[0].Value = "下线AGV到位";
                    MainForm.mainForm.dgSinal.Rows[index].Cells[1].Value = "0";
                    MainForm.mainForm.dgSinal.Rows[index].Cells[2].Value = "强制1";
                    MainForm.mainForm.dgSinal.Rows[index].Cells[3].Value = "强制0";
                    MainForm.mainForm.dgSinal.Rows[index].DefaultCellStyle.ForeColor = Color.White;
                }
                else if (i == 8)
                {
                    int index = MainForm.mainForm.dgSinal.Rows.Add();
                    MainForm.mainForm.dgSinal.Rows[index].Cells[0].Value = "下线AGV离开";
                    MainForm.mainForm.dgSinal.Rows[index].Cells[1].Value = "0";
                    MainForm.mainForm.dgSinal.Rows[index].Cells[2].Value = "强制1";
                    MainForm.mainForm.dgSinal.Rows[index].Cells[3].Value = "强制0";
                    MainForm.mainForm.dgSinal.Rows[index].DefaultCellStyle.ForeColor = Color.White;
                }
                else if (i == 9)
                {
                    int index = MainForm.mainForm.dgSinal.Rows.Add();
                    MainForm.mainForm.dgSinal.Rows[index].Cells[0].Value = "下线AGV故障";
                    MainForm.mainForm.dgSinal.Rows[index].Cells[1].Value = "0";
                    MainForm.mainForm.dgSinal.Rows[index].Cells[2].Value = "强制1";
                    MainForm.mainForm.dgSinal.Rows[index].Cells[3].Value = "强制0";
                    MainForm.mainForm.dgSinal.Rows[index].DefaultCellStyle.ForeColor = Color.White;
                }
                else if (i == 10)
                {
                    int index = MainForm.mainForm.dgSinal.Rows.Add();
                    MainForm.mainForm.dgSinal.Rows[index].Cells[0].Value = "下线机构故障";
                    MainForm.mainForm.dgSinal.Rows[index].Cells[1].Value = "0";
                    MainForm.mainForm.dgSinal.Rows[index].Cells[2].Value = "强制1";
                    MainForm.mainForm.dgSinal.Rows[index].Cells[3].Value = "强制0";
                    MainForm.mainForm.dgSinal.Rows[index].DefaultCellStyle.ForeColor = Color.White;
                }
                else if (i == 11)
                {
                    int index = MainForm.mainForm.dgSinal.Rows.Add();
                    MainForm.mainForm.dgSinal.Rows[index].Cells[0].Value = "下线AGV请求进入";
                    MainForm.mainForm.dgSinal.Rows[index].Cells[1].Value = "0";
                    MainForm.mainForm.dgSinal.Rows[index].Cells[2].Value = "强制1";
                    MainForm.mainForm.dgSinal.Rows[index].Cells[3].Value = "强制0";
                    MainForm.mainForm.dgSinal.Rows[index].DefaultCellStyle.ForeColor = Color.White;
                }

                else if (i == 12)
                {
                    int index = MainForm.mainForm.dgSinal.Rows.Add();
                    MainForm.mainForm.dgSinal.Rows[index].Cells[0].Value = "光栅信号";
                    MainForm.mainForm.dgSinal.Rows[index].Cells[1].Value = "0";
                    MainForm.mainForm.dgSinal.Rows[index].Cells[2].Value = "强制1";
                    MainForm.mainForm.dgSinal.Rows[index].Cells[3].Value = "强制0";
                    MainForm.mainForm.dgSinal.Rows[index].DefaultCellStyle.ForeColor = Color.White;
                }

            }




        }


        public void DgSinal_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            DialogResult dr = MessageBox.Show("是否强制？？？", "警告", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);

            if (dr == DialogResult.OK)
            {

                if (MainForm.mainForm.dgSinal.Columns[e.ColumnIndex].Name == "Start1")
                {
                    if (e.RowIndex == 0)
                    {
                        byte[] b = { 0, 1 };
                        mtc.MBmaster_Write(0, b);
                    }
                    else if (e.RowIndex == 1)
                    {
                        byte[] b = { 0, 1 };
                        mtc.MBmaster_Write(1, b);
                    }
                    else if (e.RowIndex == 2)
                    {
                        byte[] b = { 0, 1 };
                        mtc.MBmaster_Write(2, b);
                    }
                    else if (e.RowIndex == 3)
                    {
                        byte[] b = { 0, 1 };
                        mtc.MBmaster_Write(3, b);

                    }
                    else if (e.RowIndex == 4)
                    {
                        byte[] b = { 0, 1 };
                        mtc.MBmaster_Write(4, b);

                    }
                    else if (e.RowIndex == 5)
                    {
                        byte[] b = { 0, 1 };
                        mtc.MBmaster_Write(5, b);
                    }

                    else if (e.RowIndex == 6)
                    {
                        byte[] b = { 0, 1 };
                        mtc.MBmaster_Write(10, b);
                    }
                    else if (e.RowIndex == 7)
                    {
                        byte[] b = { 0, 1 };
                        mtc.MBmaster_Write(9, b);

                    }
                    else if (e.RowIndex == 8)
                    {
                        byte[] b = { 0, 1 };
                        mtc.MBmaster_Write(11, b);

                    }
                    else if (e.RowIndex == 9)
                    {
                        byte[] b = { 0, 1 };
                        mtc.MBmaster_Write(12, b);

                    }
                    else if (e.RowIndex == 10)
                    {
                        byte[] b = { 0, 1 };
                        mtc.MBmaster_Write(13, b);

                    }
                    else if (e.RowIndex == 11)
                    {
                        byte[] b = { 0, 1 };
                        mtc.MBmaster_Write(14, b);

                    }
                    else if (e.RowIndex == 12)
                    {
                        byte[] b = { 0, 1 };
                        mtc.MBmaster_Write(6, b);

                    }
                }
                else if (MainForm.mainForm.dgSinal.Columns[e.ColumnIndex].Name == "Start0")
                {
                    if (e.RowIndex == 0)
                    {
                        byte[] b = { 0, 0 };
                        mtc.MBmaster_Write(0, b);
                    }
                    else if (e.RowIndex == 1)
                    {
                        byte[] b = { 0, 0 };
                        mtc.MBmaster_Write(1, b);
                    }
                    else if (e.RowIndex == 2)
                    {
                        byte[] b = { 0, 0 };
                        mtc.MBmaster_Write(2, b);
                    }
                    else if (e.RowIndex == 3)
                    {
                        byte[] b = { 0, 0 };
                        mtc.MBmaster_Write(3, b);

                    }
                    else if (e.RowIndex == 4)
                    {
                        byte[] b = { 0, 0 };
                        mtc.MBmaster_Write(4, b);

                    }
                    else if (e.RowIndex == 5)
                    {
                        byte[] b = { 0, 0 };
                        mtc.MBmaster_Write(5, b);
                    }

                    else if (e.RowIndex == 6)
                    {
                        byte[] b = { 0, 0 };
                        mtc.MBmaster_Write(10, b);
                    }
                    else if (e.RowIndex == 7)
                    {
                        byte[] b = { 0, 0 };
                        mtc.MBmaster_Write(9, b);

                    }
                    else if (e.RowIndex == 8)
                    {
                        byte[] b = { 0, 0 };
                        mtc.MBmaster_Write(11, b);

                    }
                    else if (e.RowIndex == 9)
                    {
                        byte[] b = { 0, 0 };
                        mtc.MBmaster_Write(12, b);

                    }
                    else if (e.RowIndex == 10)
                    {
                        byte[] b = { 0, 0 };
                        mtc.MBmaster_Write(13, b);

                    }
                    else if (e.RowIndex == 11)
                    {
                        byte[] b = { 0, 0 };
                        mtc.MBmaster_Write(14, b);

                    }
                    else if (e.RowIndex == 12)
                    {
                        byte[] b = { 0, 0 };
                        mtc.MBmaster_Write(6, b);

                    }
                }

                MessageBox.Show("强制成功");
            }

        }


        private void BindMap()
        {
            mapCardList = mapCardBll.GetModelList("");

            if (mapCardList.Count > 0)
            {
                for (int i = 0; i < mapCardList.Count; i++)
                {
                    Label cardLbl = new Label();
                    cardLbl.Location = new Point(mapCardList[i].card_x, mapCardList[i].card_y);
                    cardLbl.Text = "";
                    cardLbl.Tag = mapCardList[i].card_id.ToString();
                    cardLbl.BackColor = Color.Fuchsia;
                    cardLbl.ForeColor = Color.White;
                    cardLbl.AutoSize = true;
                    cardLbl.Font = new Font("微软雅黑", 4f);

                    MainForm.mainForm.tpMonitor.Controls.Add(cardLbl);
                }
            }
        }


      



        /// <summary>
        /// 在某个站点上是否有AGV
        /// </summary>
        /// <returns></returns>
   
   
     

      
      
        #region AGV控制指令集
        public AgvManageForm amf;

        public TrafficManageForm tmf;

        #endregion

        #region 定时器
        public void tmTrafficRecord_Tick(object sender, EventArgs e)
        {
            //IniArrayData(TrafficManagerClass.TrafficDetailDic);
        }

        public void tmTaskShow_Tick(object sender, EventArgs e)
        {
            try
            {
                foreach (Control btn in MainForm.mainForm.tpMonitor.Controls)
                {
                    if (btn is Button)
                    {
                        int tempNum = int.Parse(btn.Text);
                        if (plcMDic.ContainsKey(tempNum))
                        {
                            if (!plcMDic[tempNum].isConnectSure)
                            {
                                MainForm.mainForm.tpMonitor.Controls.Remove(btn);

                            }
                        }

                    }
                }
            }
            catch
            { }
        }

        private void TaskDetailShow()
        {
           
        }
           

        public void tmTime_Tick(object sender, EventArgs e)
        {
            try
            {
                MainForm.mainForm.label3.Text = "当前时间：" + DateTime.Now.ToString();

               
                 MainForm.mainForm.label26.Text = "机构占用：" + mtc.DoorUpAgv + "|" + mtc.DoorDownAgv ;


                MainForm.mainForm.label27.Text = "呼叫状态："  +  boxAgv.AllowState;


                try
                {
                    MainForm.mainForm.textBox6.Text = string.Empty;
                    foreach (int item in plcMDic.Keys)
                    {
                        MainForm.mainForm.textBox6.Text += plcMDic[item].th1.ThreadState.ToString() + item.ToString() + plcMDic[item].isConnectSure.ToString() + plcMDic[item].count9.ToString() + plcMDic[item].monitorError + "\r\n";
                    }
                }
                catch (Exception ex)
                {
                    //MessageBox.Show(ex.ToString()); 
                }


                //MainForm.mainForm.label15.Text = AgvFunctionClass.TrafficSureDic[1] + "|" + AgvFunctionClass.TrafficSureDic[2];
                //string datesss=string.Format("{0:yyyyMMdd}",DateTime.Now);
                //if (AgvFunctionClass.TotalFlag !=datesss )
                //{
                //    //AgvFunctionClass.TotalFlag = datesss;
                //    //AgvFunctionClass.commandList.Clear();
                //    //AgvFunctionClass.commandList2150.Clear();
                //    //AgvFunctionClass.errorClassList.Clear();
                //    //AgvFunctionClass.jigouList.Clear();

                //    Application.Exit();
                //    Environment.Exit(0);

                //}
                string datesss = string.Format("{0:yyyyMMdd}", DateTime.Now);
                if (AgvFunctionClass.TotalFlag != datesss)
                {
                    AgvFunctionClass.TotalFlag = datesss;

                    AgvFunctionClass.stationRecordList.Clear();
                    AgvFunctionClass.commandList.Clear();
                   


                }


            }
            catch
            {

            }

        }

        public void tmPlc_Tick(object sender, EventArgs e)
        {
            try
            {
                //for (int i = 0; i < thList.Count; i++)
                //{
                //    if (thList[i].ThreadState == ThreadState.Unstarted || thList[i].ThreadState == ThreadState.Stopped)
                //    {
                //        thList[i].Start();


                //    }
                //}


                foreach (int item in plcMDic.Keys)
                {
                    if (plcMDic[item].th1.ThreadState == ThreadState.Unstarted || plcMDic[item].th1.ThreadState == ThreadState.Stopped || plcMDic[item].th1.ThreadState == ThreadState.Aborted)
                    {
                        plcMDic[item].th1 = new Thread(new ThreadStart(plcMDic[item].ChargeThreadMethod9));
                        plcMDic[item].th1.Start();


                    }
                    else if (plcMDic[item].th1.ThreadState == ThreadState.WaitSleepJoin)
                    {
                        plcMDic[item].th1.Abort();
                    }
                    //else if (plcMDic[item].th1.ThreadState == ThreadState.Aborted)
                    //{
                        
                    //}
                }

            }
            catch
            {

            }


            //TaskDetailShow();

        }

        public void tmOffine_Tick(object sender, EventArgs e)
        {
            try
            {

         
             

            }

            catch
            { }




        


        }



   
        //public void ThCon(object obj)
        //{
        //    AgvPublic.ClientTcp ct = (AgvPublic.ClientTcp)obj;

        //    ct.Reconnect();
        //}

        public void tmOkAgv_Tick(object sender, EventArgs e)
        {
            try
            {
                MainForm.mainForm.dgAgvInfo.Rows.Clear();

                foreach (int num in plcMDic.Keys)
                {
                    if (plcMDic[num].isConnectSure && plcMDic[num].chargeState9.AgvNum == num) //&& plcManager.chargeState9.CurrentStation !=0
                    {
                        int index = MainForm.mainForm.dgAgvInfo.Rows.Add();
                        MainForm.mainForm.dgAgvInfo.Rows[index].Cells[0].Value = "AGV" + plcMDic[num].chargeState9.AgvNum.ToString();
                        MainForm.mainForm.dgAgvInfo.Rows[index].Cells[1].Value = plcMDic[num].chargeState9.CurrentStation;
                        //MainForm.mainForm.dgAgvInfo.Rows[index].Cells[2].Value = "/";
                        if (plcMDic[num].chargeState9.AgvState == 1)
                        {
                            MainForm.mainForm.dgAgvInfo.Rows[index].Cells[2].Value = "运行";
                            MainForm.mainForm.dgAgvInfo.Rows[index].DefaultCellStyle.ForeColor = Color.Lime;
                        }
                        else if (plcMDic[num].chargeState9.AgvState == 0)
                        {

                            MainForm.mainForm.dgAgvInfo.Rows[index].Cells[2].Value = "停止";
                            MainForm.mainForm.dgAgvInfo.Rows[index].DefaultCellStyle.ForeColor = Color.White;

                        }
                        else if (plcMDic[num].chargeState9.AgvState == 3)
                        {

                            MainForm.mainForm.dgAgvInfo.Rows[index].Cells[2].Value = "锁车";
                            MainForm.mainForm.dgAgvInfo.Rows[index].DefaultCellStyle.ForeColor = Color.Fuchsia;

                        }
                        else if (plcMDic[num].chargeState9.AgvState == 4)
                        {

                            MainForm.mainForm.dgAgvInfo.Rows[index].Cells[2].Value = "故障";
                            MainForm.mainForm.dgAgvInfo.Rows[index].DefaultCellStyle.ForeColor = Color.Red;

                        }
                        else if (plcMDic[num].chargeState9.AgvState == 2)
                        {

                            MainForm.mainForm.dgAgvInfo.Rows[index].Cells[2].Value = "避障";
                            MainForm.mainForm.dgAgvInfo.Rows[index].DefaultCellStyle.ForeColor = Color.Gold;

                        }

                        if (plcMDic[num].chargeState9.AgvError != 0)
                        {
                            MainForm.mainForm.dgAgvInfo.Rows[index].DefaultCellStyle.ForeColor = Color.Red;
                        }


                        switch (plcMDic[num].chargeState9.AgvError)
                        {
                            case 0: MainForm.mainForm.dgAgvInfo.Rows[index].Cells[3].Value = "正常"; break;
                            case 1: MainForm.mainForm.dgAgvInfo.Rows[index].Cells[3].Value = "驱动升降错误"; break;
                            case 2: MainForm.mainForm.dgAgvInfo.Rows[index].Cells[3].Value = "牵引升降错误"; break;
                            case 4: MainForm.mainForm.dgAgvInfo.Rows[index].Cells[3].Value = "无路径错误"; break;
                            case 8: MainForm.mainForm.dgAgvInfo.Rows[index].Cells[3].Value = "通信板超时"; break;
                            case 16: MainForm.mainForm.dgAgvInfo.Rows[index].Cells[3].Value = "急停错误"; break;
                            case 32: MainForm.mainForm.dgAgvInfo.Rows[index].Cells[3].Value = "防撞错误"; break;
                            case 64: MainForm.mainForm.dgAgvInfo.Rows[index].Cells[3].Value = "驱动器故障"; break;
                            default: MainForm.mainForm.dgAgvInfo.Rows[index].Cells[3].Value = "未知异常"; break;
                        }


                        if (plcMDic[num].chargeState9.TargetStation == 1)
                        {
                            MainForm.mainForm.dgAgvInfo.Rows[index].Cells[4].Value = "SPS";
                        }
                        else if (plcMDic[num].chargeState9.TargetStation == 2)
                        {
                            MainForm.mainForm.dgAgvInfo.Rows[index].Cells[4].Value = "发变分装线";
                        }
                        else if (plcMDic[num].chargeState9.TargetStation == 3)
                        {
                            MainForm.mainForm.dgAgvInfo.Rows[index].Cells[4].Value = "仪表线";
                        }
                        else if (plcMDic[num].chargeState9.TargetStation == 4)
                        {
                            MainForm.mainForm.dgAgvInfo.Rows[index].Cells[4].Value = "车门线";
                        }
                        else if (plcMDic[num].chargeState9.TargetStation == 5)
                        {
                            MainForm.mainForm.dgAgvInfo.Rows[index].Cells[4].Value = "前滑柱分装线";
                        }
                        else if (plcMDic[num].chargeState9.TargetStation == 6)
                        {
                            MainForm.mainForm.dgAgvInfo.Rows[index].Cells[4].Value = "后轴分装线";
                        }
                        else if (plcMDic[num].chargeState9.TargetStation == 7)
                        {
                            MainForm.mainForm.dgAgvInfo.Rows[index].Cells[4].Value = "动总分装线";
                        }







                    }
                }




                MainForm.mainForm.dgAgvInfo.FirstDisplayedScrollingRowIndex = AgvFunctionClass.VerticalScrollIndex;


                MainForm.mainForm.dgAgvInfo.HorizontalScrollingOffset = AgvFunctionClass.HorizontalOffset;
             
            }

            catch
            { }


            try
            {
                MainForm.mainForm.dgSinal.Rows[0].Cells[1].Value = mtc.js.UpPlcAllowIn;
                if (mtc.js.UpPlcAllowIn == 1)
                {
                    MainForm.mainForm.dgSinal.Rows[0].DefaultCellStyle.ForeColor = Color.Lime;
                }
                else
                    MainForm.mainForm.dgSinal.Rows[0].DefaultCellStyle.ForeColor = Color.White;


                MainForm.mainForm.dgSinal.Rows[1].Cells[1].Value = mtc.js.UpAgvArrive;
                if (mtc.js.UpAgvArrive == 1)
                {
                    MainForm.mainForm.dgSinal.Rows[1].DefaultCellStyle.ForeColor = Color.Lime;
                }
                else
                    MainForm.mainForm.dgSinal.Rows[1].DefaultCellStyle.ForeColor = Color.White;

                MainForm.mainForm.dgSinal.Rows[2].Cells[1].Value = mtc.js.UpPlcLeave;
                if (mtc.js.UpPlcLeave == 1)
                {
                    MainForm.mainForm.dgSinal.Rows[2].DefaultCellStyle.ForeColor = Color.Lime;
                }
                else
                    MainForm.mainForm.dgSinal.Rows[2].DefaultCellStyle.ForeColor = Color.White;

                MainForm.mainForm.dgSinal.Rows[3].Cells[1].Value = mtc.js.UpAgvLeave;
                if (mtc.js.UpAgvLeave == 1)
                {
                    MainForm.mainForm.dgSinal.Rows[3].DefaultCellStyle.ForeColor = Color.Lime;
                }
                else
                    MainForm.mainForm.dgSinal.Rows[3].DefaultCellStyle.ForeColor = Color.White;


                MainForm.mainForm.dgSinal.Rows[4].Cells[1].Value = mtc.js.UpAgvError;
                if (mtc.js.UpAgvError == 1)
                {
                    MainForm.mainForm.dgSinal.Rows[4].DefaultCellStyle.ForeColor = Color.Lime;
                }
                else
                    MainForm.mainForm.dgSinal.Rows[4].DefaultCellStyle.ForeColor = Color.White;


                MainForm.mainForm.dgSinal.Rows[5].Cells[1].Value = mtc.js.UpPlcError;
                if (mtc.js.UpPlcError == 1)
                {
                    MainForm.mainForm.dgSinal.Rows[5].DefaultCellStyle.ForeColor = Color.Lime;
                }
                else
                    MainForm.mainForm.dgSinal.Rows[5].DefaultCellStyle.ForeColor = Color.White;

                MainForm.mainForm.dgSinal.Rows[6].Cells[1].Value = mtc.js.DownPlcAllowIn;
                if (mtc.js.DownPlcAllowIn == 1)
                {
                    MainForm.mainForm.dgSinal.Rows[6].DefaultCellStyle.ForeColor = Color.Lime;
                }
                else
                    MainForm.mainForm.dgSinal.Rows[6].DefaultCellStyle.ForeColor = Color.White;

                MainForm.mainForm.dgSinal.Rows[7].Cells[1].Value = mtc.js.DownAgvArrive;
                if (mtc.js.DownAgvArrive == 1)
                {
                    MainForm.mainForm.dgSinal.Rows[7].DefaultCellStyle.ForeColor = Color.Lime;
                }
                else
                    MainForm.mainForm.dgSinal.Rows[7].DefaultCellStyle.ForeColor = Color.White;


                MainForm.mainForm.dgSinal.Rows[8].Cells[1].Value = mtc.js.DownAgvLeave;
                if (mtc.js.DownAgvLeave == 1)
                {
                    MainForm.mainForm.dgSinal.Rows[8].DefaultCellStyle.ForeColor = Color.Lime;
                }
                else
                    MainForm.mainForm.dgSinal.Rows[8].DefaultCellStyle.ForeColor = Color.White;

                MainForm.mainForm.dgSinal.Rows[9].Cells[1].Value = mtc.js.DownAgvError;
                if (mtc.js.DownAgvError == 1)
                {
                    MainForm.mainForm.dgSinal.Rows[9].DefaultCellStyle.ForeColor = Color.Lime;
                }
                else
                    MainForm.mainForm.dgSinal.Rows[9].DefaultCellStyle.ForeColor = Color.White;



                MainForm.mainForm.dgSinal.Rows[10].Cells[1].Value = mtc.js.DownPlcError;
                if (mtc.js.DownPlcError == 1)
                {
                    MainForm.mainForm.dgSinal.Rows[10].DefaultCellStyle.ForeColor = Color.Lime;
                }
                else
                    MainForm.mainForm.dgSinal.Rows[10].DefaultCellStyle.ForeColor = Color.White;


                MainForm.mainForm.dgSinal.Rows[11].Cells[1].Value = mtc.js.DownAgvRequest;
                if (mtc.js.DownAgvRequest == 1)
                {
                    MainForm.mainForm.dgSinal.Rows[11].DefaultCellStyle.ForeColor = Color.Lime;
                }
                else
                    MainForm.mainForm.dgSinal.Rows[11].DefaultCellStyle.ForeColor = Color.White;


                MainForm.mainForm.dgSinal.Rows[12].Cells[1].Value = mtc.js.Raster;
                if (mtc.js.Raster == 1)
                {
                    MainForm.mainForm.dgSinal.Rows[12].DefaultCellStyle.ForeColor = Color.Lime;
                }
                else
                    MainForm.mainForm.dgSinal.Rows[12].DefaultCellStyle.ForeColor = Color.White;
            }
            catch { }


        }

        private void RemoveClient(string ip)
        {
            try
            {
                //for (int i = 0; i < tcpManager._clients.Count; i++)
                //{
                //    if (tcpManager._clients[i].RemoteEndPoint.ToString().Split(':')[0] == ip)
                //    {
                //        tcpManager.CloseClient(tcpManager._clients[i]);
                //        //break;
                //    }
                //}

              
            }
            catch
            { }



        
        }

      
        /// <summary>
        /// 移除图片控件
        /// </summary>
        /// <param name="ip"></param>
        //private void RemoveOfflineAgvPic(string ip)
        //{


        //    foreach (Control btn in MainForm.mainForm.tpMonitor.Controls)
        //    {
        //        if (btn is Button)
        //        {
        //            if (btn.Text == "AGV" + agvFunc.IpAndAgvinfoDic[ip].Agv_Num)
        //            {
        //                MainForm.mainForm.tpMonitor.Controls.Remove(btn);
        //                break;
        //            }
        //        }
        //    }
        //}



        /// <summary>
        /// 移除数据（必要）
        /// </summary>
        private void RemoveDicData(string ip)
        {
            try
            {
                //if(agvFunc.IpAndAgvinfoDic.ContainsKey(ip))
                //agvFunc.IpAndAgvinfoDic.Remove(ip);


                if (agvFunc.HeartbeatDic.ContainsKey(ip))
                agvFunc.HeartbeatDic.Remove(ip);
            }
            catch
            { }
          
        }
        #endregion

        #region 按钮事件

        public void amf_AddEvent(int id, string ip, int line)
        {
            try
            {
                Model.Agv_Device agvD = new Model.Agv_Device();
                agvD.agv_id = id;
                agvD.agv_ip = ip;
                agvD.agv_workline = line;
                agvDeviceBll.Add(agvD);

                MainForm.mainForm.dgLine.Rows.Clear();
                BindAgv();

                MessageBox.Show("新增成功");
            }
            catch
            {
                MessageBox.Show("编号与IP地址不能重复！");
            }
        }


        public void tmf_AddEvent(int id, int ip, string line)
        {
            try
            {
                Model.Traffic_Manager trafficM = new Model.Traffic_Manager();
                trafficM.traffic_area_no = id;
                trafficM.lock_card_num = ip;
                trafficM.except_card_for_unlock = line;
                trafficM.card_direction = 1;
                trafficManagerBll.Add(trafficM);

                MainForm.mainForm.dgTraffic.Rows.Clear();
                BindTraffic();

                MessageBox.Show("新增成功");
            }
            catch
            {
                MessageBox.Show("编号与IP地址不能重复！");
            }
        }

        public void tmf_UpdateEvent(int id, int ip, string line)
        {
            try
            {
                for (int i = 0; i < trafficManagerList.Count; i++)
                {
                    if (trafficManagerList[i].id == int.Parse(MainForm.mainForm.dgTraffic.Rows[MainForm.mainForm.selectIndexDgTraffic].Cells[4].Value.ToString()))
                    {
                        trafficManagerList[i].traffic_area_no = id;
                        trafficManagerList[i].lock_card_num = ip;
                        trafficManagerList[i].except_card_for_unlock = line;

                        trafficManagerBll.Update(trafficManagerList[i]);
                        MessageBox.Show("更新成功");
                        tmf.Close();
                        break;
                    }
                }
                MainForm.mainForm.dgTraffic.Rows.Clear();
                BindTraffic();
            }
            catch
            {
                MessageBox.Show("编号与IP地址不能重复！");
            }
        }


        public void amf_UpdateEvent(int id, string ip, int line)
        {
            try
            {
                for (int i = 0; i < agvDeviceList.Count; i++)
                {
                    if (agvDeviceList[i].agv_id == int.Parse(MainForm.mainForm.dgLine.Rows[MainForm.mainForm.selectIndexDgLine].Cells[0].Value.ToString()))
                    {
                        agvDeviceList[i].agv_id = id;
                        agvDeviceList[i].agv_ip = ip;
                        agvDeviceList[i].agv_workline = line;

                        agvDeviceBll.Update(agvDeviceList[i]);
                        MessageBox.Show("更新成功");
                        amf.Close();
                        break;
                    }
                }
                MainForm.mainForm.dgLine.Rows.Clear();
                BindAgv();
            }
            catch
            {
                MessageBox.Show("编号与IP地址不能重复！");
            }
        }


        public void DeleteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                if (MainForm.mode == 1)
                {
                    int agvTempId = int.Parse(MainForm.mainForm.dgLine.Rows[MainForm.mainForm.selectIndexDgLine].Cells[0].Value.ToString());
                    if (agvDeviceBll.Exists(agvTempId))
                    {
                        agvDeviceBll.Delete(agvTempId);
                        MainForm.mainForm.dgLine.Rows.Clear();

                        BindAgv();

                        MessageBox.Show("删除成功");
                    }


                    if (MainForm.mainForm.dgLine.Rows.Count == 0)
                    {
                        amf = new AgvManageForm(1, null);
                        amf.AddEvent += amf_AddEvent;
                        amf.ShowDialog();
                    }
                }
                else
                {
                    int areaTempId = int.Parse(MainForm.mainForm.dgTraffic.Rows[MainForm.mainForm.selectIndexDgTraffic].Cells[4].Value.ToString());
                    
                        trafficManagerBll.Delete(areaTempId);
                        MainForm.mainForm.dgTraffic.Rows.Clear();

                        BindTraffic();

                        MessageBox.Show("删除成功");



                    if (MainForm.mainForm.dgTraffic.Rows.Count == 0)
                    {
                        tmf = new TrafficManageForm(1, null);
                        tmf.AddEvent += tmf_AddEvent;
                        tmf.ShowDialog();
                    }

                }

            }
            catch
            { MessageBox.Show("删除异常"); }
        }



        public void AddToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                if (MainForm.mode == 1)
                {
                    amf = new AgvManageForm(1, null);
                    amf.AddEvent += amf_AddEvent;
                    amf.ShowDialog();
                }
                else
                {
                    tmf = new TrafficManageForm(1, null);
                    tmf.AddEvent += tmf_AddEvent;
                    tmf.ShowDialog();
                }

            }
            catch
            {  }
        }


        public void UpdateToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                if (MainForm.mode == 1)
                {
                    int agvTempId = int.Parse(MainForm.mainForm.dgLine.Rows[MainForm.mainForm.selectIndexDgLine].Cells[0].Value.ToString());
                    if (agvDeviceBll.Exists(agvTempId))
                    {
                        amf = new AgvManageForm(2, MainForm.mainForm.dgLine.Rows[MainForm.mainForm.selectIndexDgLine]);
                        amf.UpdateEvent += amf_UpdateEvent;
                        amf.ShowDialog();
                    }
                }
                else
                {
                    int trafficTempId = int.Parse(MainForm.mainForm.dgTraffic.Rows[MainForm.mainForm.selectIndexDgTraffic].Cells[4].Value.ToString());
                    if (trafficManagerBll.Exists(trafficTempId))
                    {
                        tmf = new TrafficManageForm(2, MainForm.mainForm.dgTraffic.Rows[MainForm.mainForm.selectIndexDgTraffic]);
                        tmf.UpdateEvent += tmf_UpdateEvent;
                        tmf.ShowDialog();
                    }
                }
                    

                //dgLingjian.Rows[selectIndex].ReadOnly = false;
                //if (checkProBll.Exists(dgLingjian.Rows[selectIndex].Cells[2].Value.ToString()))
                //{
                //    checkProBll.Delete(dgLingjian.Rows[selectIndex].Cells[2].Value.ToString());
                //    dgLingjian.Rows.Clear();
                //    GetCodeDataDataBase();

                //    MessageBox.Show("删除成功");
                //}

            }
            catch
            { MessageBox.Show("删除异常"); }
            //string s = this.dgLingjian.Rows[index].Cells[1].Value.ToString();
            //string s1 = this.dgLingjian.Rows[index].Cells[2].Value.ToString();
            //string s2 = this.dgLingjian.Rows[index].Cells[3].Value.ToString();
            //string s3 = this.dgLingjian.Rows[index].Cells[4].Value.ToString();
            //string s4 = this.dgLingjian.Rows[index].Cells[5].Value.ToString();
        }
    




        public void btnScreen_Click(object sender, EventArgs e)
        {
            try
            {
                string directoryPath = Application.StartupPath + @"\ScreenShot";
                if (!Directory.Exists(directoryPath))//如果路径不存在
                {
                    Directory.CreateDirectory(directoryPath);//创建一个路径的文件夹

                    Bitmap bit = new Bitmap(MainForm.mainForm.Width, MainForm.mainForm.Height);//实例化一个和窗体一样大的bitmap
                    Graphics g = Graphics.FromImage(bit);
                    g.CompositingQuality = CompositingQuality.HighQuality;//质量设为最高
                    g.CopyFromScreen(MainForm.mainForm.Left, MainForm.mainForm.Top, 0, 0, new Size(MainForm.mainForm.Width, MainForm.mainForm.Height));//保存整个窗体为图片
                    //g.CopyFromScreen(panel游戏区 .PointToScreen(Point.Empty), Point.Empty, panel游戏区.Size);//只保存某个控件（这里是panel游戏区）
                    bit.Save(Application.StartupPath + "\\ScreenShot\\" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".png");//默认保存格式为PNG，保存成jpg格式质量不是很好

                    MessageBox.Show("截图保存成功，路径为" + Application.StartupPath + "\\ScreenShot\\");
                }
                else
                {
                    Bitmap bit = new Bitmap(MainForm.mainForm.Width, MainForm.mainForm.Height);//实例化一个和窗体一样大的bitmap
                    Graphics g = Graphics.FromImage(bit);
                    g.CompositingQuality = CompositingQuality.HighQuality;//质量设为最高
                    g.CopyFromScreen(MainForm.mainForm.Left, MainForm.mainForm.Top, 0, 0, new Size(MainForm.mainForm.Width, MainForm.mainForm.Height));//保存整个窗体为图片
                    //g.CopyFromScreen(panel游戏区 .PointToScreen(Point.Empty), Point.Empty, panel游戏区.Size);//只保存某个控件（这里是panel游戏区）
                    bit.Save(Application.StartupPath + "\\ScreenShot\\" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".png");//默认保存格式为PNG，保存成jpg格式质量不是很好

                    MessageBox.Show("截图保存成功，保存路径为" + Application.StartupPath + "\\ScreenShot\\");
                }
            }
            catch
            {
                MessageBox.Show("截图保存异常，请稍后重试");
            }

        }

  

        public void btnUnlock_Click(object sender, EventArgs e)
        {
            try
            {
               

            }
            catch
            {
            }

        }

    

      

        //private void IniArrayData(Dictionary<int,TrafficDetailClass> TrafficDataListTemp)
        //{
        //    if (MainForm.mainForm.dgCheck.InvokeRequired)
        //    {
        //        try
        //        {
        //            CommunicateTrafficMonitorCallBack communicateCallBack = new CommunicateTrafficMonitorCallBack(IniArrayData);
        //            MainForm.mainForm.dgCheck.BeginInvoke(communicateCallBack, new object[] { TrafficDataListTemp });

        //        }
        //        catch
        //        {

        //        }

        //    }
        //    else
        //    {
        //        MainForm.mainForm.dgCheck.Rows.Clear();

        //        for (int i = 1; i < TrafficDataListTemp.Count + 1; i++)
        //        {
        //            int index = MainForm.mainForm.dgCheck.Rows.Add();
        //            MainForm.mainForm.dgCheck.Rows[index].DefaultCellStyle.ForeColor = Color.Gold;
        //            MainForm.mainForm.dgCheck.Rows[index].Cells[0].Value = TrafficDataListTemp[i].TrafficAreaNum;
        //            //MainForm.mainForm.dgCheck.Rows[index].Cells[1].Value = plcMDic[num].chargeState9.CurrentStation;
        //            //MainForm.mainForm.dgAgvInfo.Rows[index].Cells[2].Value = "/";

        //            for (int j = 0; j < 3; j++)
        //            {
        //                if (TrafficDataListTemp[i].TrafficInArray[j] == 0)
        //                {
        //                    MainForm.mainForm.dgCheck.Rows[index].Cells[j + 1].Value = "-";
        //                }
        //                else
        //                    MainForm.mainForm.dgCheck.Rows[index].Cells[j + 1].Value = TrafficDataListTemp[i].TrafficInArray[j].ToString();
        //            }

        //            for (int j = 0; j < 5; j++)
        //            {
        //                if (TrafficDataListTemp[i].TrafficWaitArray[j] == 0)
        //                {
        //                    MainForm.mainForm.dgCheck.Rows[index].Cells[j + 4].Value = "-";
        //                }
        //                else
        //                    MainForm.mainForm.dgCheck.Rows[index].Cells[j + 4].Value = TrafficDataListTemp[i].TrafficWaitArray[j].ToString();
        //            }
        //            //MainForm.mainForm.trafficMonitor.lvTask.Items.Add(lvi);
        //        }
        //        MainForm.mainForm.dgCheck.FirstDisplayedScrollingRowIndex = AgvFunctionClass.VerticalScrollIndex1;


        //        MainForm.mainForm.dgCheck.HorizontalScrollingOffset = AgvFunctionClass.HorizontalOffset1;


        //    }

        //}

        private void TrafficMonitorReset(int num)
        {
            TrafficManagerClass.ClearTrafficArea(num);
        }
    
        #region 工位处理增删隐
        List<Label> stationLbl1 = new List<Label>();
        int pointX1, pointY1, pointX2, pointY2;

        public void tpMonitor_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                pointX1 = e.X;
                pointY1 = e.Y;
            }
        }

        private void GetStationLbl()
        {

            stationLbl1.Clear();
            foreach (Control item in MainForm.mainForm.tpMonitor.Controls)
            {
                if (item is Label)
                {
                    stationLbl1.Add((Label)item);
                }
            }
        }

        public void ToolStripMenuItemAdd_Click(object sender, EventArgs e)
        {

            StationAddForm saf = new StationAddForm( pointX1, pointY1);
            saf.AddCardId += AddCard;
            saf.ShowDialog();
        }

        public void AddCard(int x, int y, int cardId)
        {
            Label cardLbl = new Label();
            cardLbl.Location = new Point(x, y);
            cardLbl.Text = " ";
            cardLbl.Tag = cardId.ToString();
            cardLbl.BackColor = Color.Fuchsia;
            cardLbl.ForeColor = Color.White;
            cardLbl.AutoSize = true;
            cardLbl.Font = new Font("微软雅黑", 4f);

            MainForm.mainForm.tpMonitor.Controls.Add(cardLbl);

            Model.Map_Card mc = new Model.Map_Card();
            mc.card_id = cardId;
            mc.card_x = x;
            mc.card_y = y;
            mapCardBll.Add(mc);

        }
        

      

        public void DelCard(int cardId)
        {
            foreach (Control lbl in MainForm.mainForm.tpMonitor.Controls)
            {
                if (lbl is Label)
                {
                    if ((int)lbl.Tag == cardId)
                    {
                        MainForm.mainForm.tpMonitor.Controls.Remove(lbl);
                        break;
                    }
                }

            }
        }




        #endregion

        public void button35_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show("是否复位？", "警告", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);

            if (dr == DialogResult.OK)
            {
                mtc.DoorUpAgv = 0;
            }
           
        }
        public void button36_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show("是否复位？", "警告", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);

            if (dr == DialogResult.OK)
            {
                mtc.DoorDownAgv = 0;
            }

           
        }


        public void button37_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show("是否占用？", "警告", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);

            if (dr == DialogResult.OK)
            {
                mtc.DoorDownAgv = int.Parse(MainForm.mainForm.textBox15.Text); ;
            }


        }


        public void button38_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show("是否占用？", "警告", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);

            if (dr == DialogResult.OK)
            {
                mtc.DoorUpAgv = int.Parse(MainForm.mainForm.textBox15.Text); ;
            }


        }



        public void StartRunToolStripMenuItems_Click(object sender, EventArgs e)
        {
            try
            {
                //if (MainForm.mainForm.StartRunToolStripMenuItem.Text == "启动调度")
                //{
                //    //agvFunc.disPatchControl = true;
                //    MainForm.mainForm.StartRunToolStripMenuItem.Text = "关闭调度";
                //}
                //else
                //{
                //    DialogResult dr = MessageBox.Show("关闭调度？", "警告", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);

                //    if (dr == DialogResult.OK)
                //    {
                //        //agvFunc.disPatchControl = false;
                //        //MainForm.mainForm.StartRunToolStripMenuItem.Text = "启动调度";
                //    }

                //}
            }
            catch
            {
                MessageBox.Show("复位异常");//+ agvFunc.PlcName[13]
            }
        }
        #endregion
    }
}
